package com.shujia.flink.tf;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

public class Demo8ProcessFunction {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> linesDS = env.socketTextStream("master", 8888);

        /*
         * process算子是flink的底层算子吗，可以代替map flatMap filter算子
         */
        DataStream<Tuple2<String, Integer>> kvDS = linesDS
                .process(new ProcessFunction<String, Tuple2<String, Integer>>() {
                    /**
                     * processElement:相当于flatMap。每一条数据执行一次，可以返回一条也可以返回多条
                     * line：一行数据
                     * ctx：上下文对象（代表flink的执行环境）
                     * out： 用于将数据发送到下游
                     */
                    @Override
                    public void processElement(String line,
                                               ProcessFunction<String, Tuple2<String, Integer>>.Context ctx,
                                               Collector<Tuple2<String, Integer>> out) throws Exception {
                        for (String word : line.split(",")) {
                            out.collect(Tuple2.of(word, 1));
                        }
                    }
                });

        kvDS.print();


        env.execute();
    }
}
